package com.miaoke;

import com.miaoke.entity.RpcTransferDto;

import java.lang.reflect.Method;

public class ServiceDispatch {
    public static Object dispatch(Object obj){
        RpcTransferDto rpcTransferDto=(RpcTransferDto) obj;
        String className = rpcTransferDto.getClassName();
        String methodName = rpcTransferDto.getMethodName();
        Object[] params = rpcTransferDto.getParams();
        Class[] paramTypes=new Class[params.length];
        int i=0;
        for (Object param:params){
            Class paramClass = param.getClass();
            paramTypes[i]=paramClass;
            i++;
        }
        Object retObj=null;
        try {
            Class clazz = Class.forName(className);
            Method method = clazz.getMethod(methodName, paramTypes);
            retObj = method.invoke(clazz.newInstance(), params);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return retObj;
    }
}
